package com.fatdetection.demo.service.imp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fatdetection.demo.bean.Admin;
import com.fatdetection.demo.mapper.AdminMapper;
import com.fatdetection.demo.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;
import java.util.List;

@Service
public class AdminServiceImp implements AdminService {

    @Autowired
    AdminMapper adminMapper;

    @Override
    public Page<Admin> selectPage(Integer current, Integer size) {
        Page<Admin> page = new Page<>(current, size);
        return adminMapper.selectPage(page, null);

    }

    @Override
    public boolean updatePassword(String oldPwd, String newPwd, String username) {
        boolean status = false;
        if(login(username, oldPwd) != null){
            // 验证通过可以修改密码
            Admin admin = new Admin();
            admin.setPassword(newPwd);
            admin.setUsername(username);
            if(updateAdmin(admin) > 0){
                // 修改成功
                status = true;
            } else {
                // 修改失败
            }
        } else {
            // 密码验证失败
        }
        return status;
    }

    @Override
    public int registerAdmin(Admin admin) {
        admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes()));
        //判断用户名是否已被注册
        if(findAdminByusername(admin.getUsername()) != null){
            return -2;//状态码-2表示用户名已被注册
        }
        return adminMapper.insert(admin);
    }

    @Override
    public int delAdmin(String username) {
        return adminMapper.delete(new QueryWrapper<Admin>().eq("username", username));
    }

    @Override
    public int updateAdmin(Admin admin) {
        if(admin.getPassword() != null){
            admin.setPassword(DigestUtils.md5DigestAsHex(admin.getPassword().getBytes(StandardCharsets.UTF_8)));
        }
        return adminMapper.update(admin, new UpdateWrapper<Admin>().eq("username", admin.getUsername()));
    }

    @Override
    public Admin login(String username, String password) {
        String pwd = DigestUtils.md5DigestAsHex(password.getBytes());
        return adminMapper.selectOne(new QueryWrapper<Admin>()
                .eq("username", username)
                .eq("password", pwd));
    }

    @Override
    public List<Admin> findAll() {
        return adminMapper.selectList(null);
    }

    @Override
    public Admin findAdminByusername(String username) {
        return adminMapper.selectOne(new QueryWrapper<Admin>().eq("username", username));
    }

    @Override
    public List<Admin> findAdminByNickname(String nickname) {
        return adminMapper.selectList(new QueryWrapper<Admin>().eq("nickname", nickname));
    }

}
